package com.djinfo.work.service.impl;

import com.djinfo.work.common.bean.AjaxJson;
import com.djinfo.work.common.service.impl.BaseServiceImpl;
import com.djinfo.work.common.util.StringUtil;
import com.djinfo.work.dao.DepartmentDao;
import com.djinfo.work.dao.NoticeDao;
import com.djinfo.work.dao.UserDao;
import com.djinfo.work.dao.impl.NoticeDaoImpl;
import com.djinfo.work.entity.Department;
import com.djinfo.work.entity.SysNoticeEntity;
import com.djinfo.work.entity.TbSysUsers;
import com.djinfo.work.service.AttachmentService;
import com.djinfo.work.service.NoticeDepartService;
import com.djinfo.work.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;


/**
 *
 */
@Service("NoticeService")
public class NoticeServiceImpl extends BaseServiceImpl<SysNoticeEntity,NoticeDaoImpl> implements NoticeService {

	@Autowired
	private NoticeDao noticeDao;

	@Autowired
	private UserDao userDao;

	@Autowired
	private DepartmentDao departmentDao;

	@Autowired
	private AttachmentService attachmentService;

	@Autowired
	private NoticeDepartService noticeDepartService;

	@Override
	public SysNoticeEntity getNoticeById(String id) {
		return noticeDao.getNoticeById(id);
	}

	@Override
	public List<SysNoticeEntity> page(SysNoticeEntity sysNoticeEntity, Integer pageNum, Integer pageSize) {
		return noticeDao.page(sysNoticeEntity,  pageNum,  pageSize);
	}

	@Override
	public List<SysNoticeEntity> list() {
		return noticeDao.list();
	}

	@Override
	public Integer getAllRowCount() {
		return noticeDao.getAllRowCount();
	}

	@Override
	public void updateStatus(String id,HttpSession session) {
		noticeDao.updateStatus(id,session);
	}

	@Override
	public Integer getCountByCondition(SysNoticeEntity object) {
		return noticeDao.getCountByCondition(object);
	}

	/**
	 * 获取部门列表和当前登录人名称
	 * @param session
     * @return
     */
	@Override
	public AjaxJson getInitInfo(HttpSession session) {
		AjaxJson ajaxJson = new AjaxJson();
		HashMap<String, Object> map = new HashMap<>();
		String userid = (String) session.getAttribute("userId");
		TbSysUsers tbSysUsers = userDao.get(userid);
		List<Department> departmentList1 = departmentDao.getDepartsBy("0");
		int size = departmentList1.size();
		for(int i=0;i<departmentList1.size();i++){
			String id = departmentList1.get(i).getId();
			List<Department> departmentList2= departmentDao.getDepartsBy(id);
			if(departmentList2.size()>=0){
				departmentList1.get(i).setDepartmentList(departmentList2);
			}
		}
		if(StringUtil.isNotEmpty(departmentList1)){

		}else{
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
		}
		map.put("username", tbSysUsers.getUserName());
		map.put("list",departmentList1);
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	/**
	 * 公告列表
	 * @param sysNoticeEntity
	 * @param pagenum
	 * @param pagesize
     * @return
     */
	@Override
	public AjaxJson list(SysNoticeEntity sysNoticeEntity, Integer pagenum, Integer pagesize) {
		HashMap<String, Object> map = new HashMap<>();
		AjaxJson ajaxJson = new AjaxJson();
		try{
			List<SysNoticeEntity> res = noticeDao.page(sysNoticeEntity,pagenum,pagesize);
			Integer count=0;
			if(StringUtil.isNotEmpty(sysNoticeEntity.getTitle())||StringUtil.isNotEmpty(sysNoticeEntity.getStatus())){
				count = noticeDao.getCountByCondition(sysNoticeEntity);
			}else{
				count = noticeDao.getAllRowCount();
			}
			map.put("dataList",res);
			map.put("total",count);
		}catch (Exception e){
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		ajaxJson.setResult(map);
		return ajaxJson;
	}

	/**
	 * 去新增页面
	 * @param sysNoticeEntity
     * @return ModelAndView
     */
	@Override
	public ModelAndView redirect(SysNoticeEntity sysNoticeEntity) {
		if(sysNoticeEntity.getId()==null){
			sysNoticeEntity = new SysNoticeEntity();
		}else{
			sysNoticeEntity = this.getEntity(SysNoticeEntity.class, sysNoticeEntity.getId());
		}
		return new ModelAndView("notice/createNotice.html","notice", sysNoticeEntity);
	}

	/**
	 * 发表或者更新公告
	 * @param sysNoticeEntity
	 * @param session
     * @return
     */
	@Override
	public AjaxJson save(SysNoticeEntity sysNoticeEntity, HttpSession session) {
		AjaxJson ajaxJson = new AjaxJson();
		String nowTime = StringUtil.getNowTime();
		String userid = (String) session.getAttribute("userId");
		TbSysUsers tbSysUsers = userDao.get(userid);
		String id = tbSysUsers.getPkUserId();
		String name = tbSysUsers.getUserName();
		try{
			if(StringUtil.isNotEmpty(sysNoticeEntity.getId())){
				sysNoticeEntity.setUpdateTime(nowTime);
				sysNoticeEntity.setUpdateUser(id);
				attachmentService.delSomeData(sysNoticeEntity.getId());
				int lengths= sysNoticeEntity.getAttachMent().length();
				attachmentService.updateAttach(sysNoticeEntity,lengths,nowTime,id);
				noticeDepartService.delSomeData(sysNoticeEntity.getId());
				noticeDepartService.updateDepart(sysNoticeEntity,nowTime,id);
				this.update(sysNoticeEntity);
			}else{
				sysNoticeEntity.setCreateTime(nowTime);
				sysNoticeEntity.setUpdateTime(nowTime);
				sysNoticeEntity.setCreateUser(id);
				sysNoticeEntity.setUpdateUser(id);
				sysNoticeEntity.setPublish(name);
				sysNoticeEntity.setStatus(0);
				this.save(sysNoticeEntity);
				attachmentService.saveAttach(sysNoticeEntity,nowTime,id);
				noticeDepartService.saveDepart(sysNoticeEntity,nowTime,id);
			}
		}catch (Exception e){
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	/**
	 * 更新公告
	 * @param sysNoticeEntity
	 * @param session
     * @return
     */
	@Override
	public AjaxJson update(SysNoticeEntity sysNoticeEntity, HttpSession session) {
		AjaxJson ajaxJson = new AjaxJson();
		String userid = (String) session.getAttribute("userId");
		TbSysUsers tbSysUsers = userDao.get(userid);
		String id = tbSysUsers.getPkUserId();
		String nowTime = StringUtil.getNowTime();
		sysNoticeEntity.setUpdateTime(nowTime);
		sysNoticeEntity.setUpdateUser(id);
		try{
			this.update(sysNoticeEntity);
		}catch(Exception e){
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	/**
	 * 删除公告
	 * @param sysNoticeEntity
     * @return AjaxJson
     */
	@Override
	public AjaxJson deletes(SysNoticeEntity sysNoticeEntity) {
		AjaxJson ajaxJson = new AjaxJson();
		String id = sysNoticeEntity.getId();
		try{
			attachmentService.delSomeData(id);
			noticeDepartService.delSomeData(id);
			this.delete(sysNoticeEntity);
		}catch (Exception e){
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	/**
	 * 批量删除
	 * @param ids
	 * @return AjaxJson
     */
	@Override
	public AjaxJson deleteSome(String ids) {
		AjaxJson ajaxJson = new AjaxJson();
		try{
			String[] a = ids.split(",");
			for(int i=0;i<a.length;i++){
				SysNoticeEntity res = this.getEntity(SysNoticeEntity.class,a[i]);
				attachmentService.delSomeData(a[i]);
				noticeDepartService.delSomeData(a[i]);
				this.delete(res);
			}
		}catch (Exception e){
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}

	/**
	 * 修改公告状态
	 * @param id
	 * @param session
     * @return AjaxJson
     */
	@Override
	public AjaxJson updatestatus(String id, HttpSession session) {
		AjaxJson ajaxJson = new AjaxJson();
		try{
			noticeDao.updateStatus(id,session);
		}catch (Exception e){
			ajaxJson.setCode(201);
			ajaxJson.setMsg("error");
			e.printStackTrace();
		}
		return ajaxJson;
	}
}
